package cn.xspace.wnx.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.imcommon.utils.AppUtil;
import com.xspace.android.xafcommon.util.HeadShowUtil;
import com.xspace.android.xafcommon.util.HypyUtil;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.view.RoundAngleImageView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

import cn.xspace.wnx.R;
import cn.xspace.wnx.entity.ServiceObjBean;
import cn.xspace.wnx.view.QuickAlphabeticBar;

/**
 * zhuyuhuan 创建于 2017/3/17 0017.
 */
public class ServiceObjAdapter extends BaseAdapter {

    private static final String TAG = ServiceObjAdapter.class.getSimpleName();

    private Context mContext;
    private QuickAlphabeticBar alphabeticBar;
    private List<ServiceObjBean> mDataList;
    private boolean isShowAlpha = true;

    public ServiceObjAdapter(Context context, List<ServiceObjBean> mDataList, QuickAlphabeticBar alpha) {
        this.mDataList = mDataList;
        mContext = context;
        alphabeticBar = alpha;
        if (alpha == null) {
            return;
        }
        initAlpha(mDataList);
    }

    public void initAlpha(List<ServiceObjBean> list) {
        LogUtil.d(list.toString());
        TreeMap<String, Integer> alphaIndexer = new TreeMap<>();
        for (int i = 0; i < list.size(); i++) {
            ServiceObjBean info = list.get(i);
            if (!TextUtils.isEmpty(HypyUtil.cn2py(info.user_name))) {
                try {
                    Integer.parseInt(HypyUtil.cn2py(info.user_name));
                    if (!alphaIndexer.containsKey("#")) {
                        alphaIndexer.put("#", i);
                    }
                } catch (Exception e) {
                    if (!alphaIndexer.containsKey(HypyUtil.cn2py(info.user_name.toUpperCase()))) {
                        alphaIndexer.put(HypyUtil.cn2py(info.user_name).toUpperCase(), i);
                    }
                }
            } else {
                if (!alphaIndexer.containsKey("#")) {
                    alphaIndexer.put("#", i);
                }
            }
        }
        LogUtil.d(TAG, "alphaIndexer >>" + alphaIndexer.toString());
        Set<String> sectionLetters = alphaIndexer.keySet();
        ArrayList<String> sectionList = new ArrayList<>(sectionLetters);
        Collections.sort(sectionList);
        String[] sections = new String[sectionList.size()];
        sectionList.toArray(sections);
        alphabeticBar.setAlphaIndexer(alphaIndexer);
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mDataList.get(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ServiceObjBean mFriend = mDataList.get(position);
        ServiceObjAdapter.ViewHolder mViewHolder;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.service_obj_item, null);
            mViewHolder.findView(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        setData(mViewHolder, mFriend, position);
        return convertView;
    }

    private void setData(ViewHolder viewHolder, ServiceObjBean bean, int position) {
        HeadShowUtil.headShow(mContext, bean.user_logo, bean.user_sex, viewHolder
                .headerIV, HeadShowUtil.HeadType.UserLogo);
        viewHolder.serviceObjName.setText(bean.user_name);
        viewHolder.serviceObjTypeName.setText(bean.object_typename);
        if (!TextUtils.isEmpty(bean.user_address)) {
            viewHolder.userAddress.setText(bean.user_address);
        } else {
            StringBuilder address = new StringBuilder();
            if (!TextUtils.isEmpty(bean.user_quname)) {
                address.append(bean.user_quname);
            }
            if (!TextUtils.isEmpty(bean.user_zhenname)) {
                address.append(bean.user_zhenname);
            }
            if (!TextUtils.isEmpty(bean.user_shename)) {
                address.append(bean.user_shename);
            } else if (!TextUtils.isEmpty(bean.user_cunname)) {
                address.append(bean.user_cunname);
            }

            viewHolder.userAddress.setText(address.toString());
        }
        //viewHolder.contactItemAlphaTV.setText(HypyUtil.cn2py(list.get(position).user_name));
        bean.shotKey = HypyUtil.cn2py(bean.user_name);
        if (isShowAlpha) {
            String currentStr = AppUtil.getAlpha(HypyUtil.cn2py(bean.shotKey));
            String previewStr = (position - 1) >= 0 ? AppUtil.getAlpha(mDataList.get(position - 1).shotKey) : " ";
            if (!previewStr.equals(currentStr)) {
                viewHolder.contactItemAlphaTV.setVisibility(View.VISIBLE);
                viewHolder.contactItemAlphaTV.setText(currentStr);
            } else {
                viewHolder.contactItemAlphaTV.setVisibility(View.GONE);
            }
        } else {
            viewHolder.contactItemAlphaTV.setVisibility(View.GONE);
        }
    }

    static class ViewHolder {
        ImageView headerIV;
        TextView contactItemAlphaTV;
        TextView serviceObjName;
        TextView serviceObjTypeName;
        TextView userAddress;
        ImageView mLineImg;

        public void findView(View v) {
            this.headerIV = (RoundAngleImageView) v.findViewById(R.id.riv_service_obj_head);
            this.contactItemAlphaTV = (TextView) v.findViewById(R.id.contact_list_item_alpha_service);
            this.serviceObjName = (TextView) v.findViewById(R.id.tv_service_obj_name);
            this.serviceObjTypeName = (TextView) v.findViewById(R.id.tv_object_typename);
            this.userAddress = (TextView) v.findViewById(R.id.tv_user_address);
            this.mLineImg = (ImageView) v.findViewById(R.id.line_ing_service);
        }
    }
}